﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace MGFramework{
    public class ConvertGenericBinary {
        private static string dic = "jez0m3t7h9fdc61pg8ibk4anolqrv5uswxy2ARGTEMCHPJZXFNBOQYSDUVWLIK";
        private static char[] Base62Code = { 'j', 'e', 'z', '0', 'm', '3', 't', '7', 'h', '9', 'f', 'd', 'c', '6', '1', 'p', 'g', '8', 'i', 'b', 'k', '4', 'a', 'n', 'o', 'l', 'q', 'r', 'v', '5', 'u', 's', 'w', 'x', 'y', '2'
        , 'A', 'R', 'G', 'T', 'E', 'M', 'C', 'H', 'P', 'J', 'Z', 'X', 'F', 'N', 'B', 'O', 'Q', 'Y', 'S', 'D', 'U', 'V', 'W', 'L', 'I', 'K'};
        /// <summary>
        /// 将32进制数转换为10进制数
        /// </summary>
        /// <param name="value">32进制数</param>
        /// <returns>返回13位long类型10进制整数</returns>
        public static long To10(string value)
        {
            //Debug.LogError(se);
            var result = value.Aggregate(0L, (sum, c) => sum * 32 + dic.IndexOf(c));
            return result;
        }

        /// <summary>
        /// 将10进制数转换为32进制数
        /// </summary>
        /// <param name="value">10进制数</param>
        /// <returns>返回9位string类型32进制整数</returns>
        public static string To32(long value)
        {
            if (value == 100000)
            {
                return "System";
            }

            string result = "";
            while (value >= 1)
            {
                int index = Convert.ToInt16(value - (value / 32) * 32);
                result = Base62Code[index] + result;
                value = value / 32;
            }
            return result;
        }

        /// <summary>
        /// 将62进制转换为10进制
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static long Code62To10(string value)
        {
            var result = value.Aggregate(0L, (sum, c) => sum * 62 + dic.IndexOf(c));
            return result;
        }

        /// <summary>
        /// 将10进制转换为62进制
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string To62(long value) {
            if (value == 100000)
            {
                return "System";
            }

            string result = "";
            while (value >= 1)
            {
                int index = Convert.ToInt16(value - (value / 62) * 62);
                result = Base62Code[index] + result;
                value = value / 62;
            }
            return result;
        }

        /// <summary>
        /// int转byte数组
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static byte[] IntToByteArray(int num) {
            return new byte[] {
                (byte)((num>>24)&0xFF),
                (byte)((num>>16)&0xFF),
                (byte)((num>>8)&0xFF),
                (byte)(num&0xFF)
            };
        }
        /// <summary>
        /// byte数组转int
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static int ByteArrayTint(byte[] bytes)
        {
            return bytes[3] & 0xFF |
                   (bytes[2] & 0xFF) << 8 |
                   (bytes[1] & 0xFF) << 16 |
                   (bytes[0] & 0xFF) << 24;
        }
    }

}
